package com.mark.springsecurity.security;

import com.mark.springsecurity.domain.SysUser;
import com.mark.springsecurity.repository.SysUserRepository;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import java.util.Optional;

public class CustomUserDetailsService implements UserDetailsService {
  private final SysUserRepository sysUserRepository;

  public CustomUserDetailsService(SysUserRepository sysUserRepository) {
    this.sysUserRepository = sysUserRepository;
  }

  @Override
  public UserDetails loadUserByUsername(String username) {
    Optional<SysUser> sysUserOptional =
        sysUserRepository.findByUsernameAndEnabled(username, Boolean.TRUE); // 2
    return sysUserOptional.orElseThrow(
        () -> new UsernameNotFoundException("Username not found")); //
  }
}
